require "import"
import "tools"
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS).setStatusBarColor(0xff4DB6AC)
activity.setContentView("vConcat_")
oDir.text=LUA_SD.."/avHandle/output/"
do_concat.background=btBackground
page.addOnPageChangeListener{
  onPageScrolled=function(a,b,c)
    bar.setX((a+b)*activity.getWidth()/2)
end}
concat.onClick=function()
  page.showPage(0)
end
cascad.onClick=function()
  page.showPage(1)
end
oDir.onClick=function()
  ChoicePath(LUA_SD,function(dir)
    oDir.text=dir.."/"
  end)
end
format={"mp4","avi","flv","asf","gif","mkv","mov","mpeg","mpg","ra","rm","wmv"}
adp=ArrayAdapter(activity,android.R.layout.simple_spinner_item, String(format))
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
sp.Adapter=adp
item=
{
  LinearLayout;
  gravity="center|left";
  orientation="vertical";
  layout_height="50dp";
  layout_width="-1";
  {
    TextView;
    layout_marginLeft="20dp";
    text="路径";
    id="dir";
  };
};
adapter=LuaAdapter(activity,item)
adapter.add{dir="点击添加视频文件",time=""}
lv.Adapter=adapter
lv.onItemClick=function(a,b,c,d)
  local s= b.Tag.dir.text
  if s=="点击添加视频文件"
    ChoiceFile(LUA_SD,function(p)
      table.insert(adapter.data,#adapter.data,{dir=p})
      adapter.notifyDataSetChanged()
    end)
   else
    local items={"删除","预览","上移","下移"}
    AlertDialog.Builder(this) .setItems(items,{onClick=function(l,v)
        local str=items[v+1]
        if str=="删除"
          table.remove(adapter.data,d)
          adapter.notifyDataSetChanged()
        end
        if str=="预览"
          dialogPlayerVideo(activity,s)
        end
        if str=="上移"
          local data =adapter.data[d]
          if d-1>0
            table.remove(adapter.data,d)
            table.insert(adapter.data,d-1,data)
            adapter.notifyDataSetChanged()
           else
            print("最上边了")
          end
        end
        if str=="下移"
          local data =adapter.data[d]
          if d+2<=#adapter.data
            table.remove(adapter.data,d)
            table.insert(adapter.data,d+1,data)
            adapter.notifyDataSetChanged()
           else
            print("最下边了")
          end
        end
    end}).show()
  end
end
do_concat. onClick=function()
  local tmpdir={}
  local concatStr=""
  if filename.text~=""
    for n=1,#adapter.data-1
      table.insert(tmpdir, "-i "..adapter.data[n].dir.." -y -vcodec copy -acodec copy "..LUA_SD.."/avHandle/tmp/".. tostring(n)..".ts" )
      if concatStr==""
        concatStr=concatStr..LUA_SD.."/avHandle/tmp/".. tostring(n)..".ts"
       else
        concatStr=concatStr.."|"..LUA_SD.."/avHandle/tmp/".. tostring(n)..".ts"
      end
    end
    table.insert(tmpdir, "-i concat:"..concatStr.. " -y -vcodec copy -acodec copy "..oDir.text..filename.text.."."..sp.getSelectedItem())
    activity.newActivity("handle",tmpdir)
   else
    print("请输入文件名")
  end
end
item_cascade=
{
  LinearLayout;
  layout_height="50dp";
  layout_width="-1";
  gravity="center|left";
  orientation="vertical";
  {
    TextView;
    id="dir";
    text="路径";
    layout_marginLeft="20dp";
  };
  {
    LinearLayout;
    layout_marginLeft="20dp";
    layout_width="-1";
    {
      TextView;
      text="位置:";
      id="tip";
    };
    {
      TextView;
      text="0x0";
      id="pos";
    };
    {
      TextView;
      layout_marginLeft="20dp";
      id="tip2";
      text="大小:";
    };
    {
      TextView;
      text="原大小";
      id="size";
    };
  };
};


adapter_cascade=LuaAdapter(activity,item_cascade)
adapter_cascade.add{dir="点击添加视频文件",pos="",size="",tip="",tip2=""}
lv_cascad.Adapter=adapter_cascade
lv_cascad.onItemClick=function(a,b,c,d)
  local direction= b.Tag.dir.text
  if direction=="点击添加视频文件"
    ChoiceFile(LUA_SD,function(p)
      if unorder.checked
        table.insert(adapter_cascade.data,#adapter_cascade.data,{dir=p,pos={text="x=0:y=0"},size={text="原大小"},tip={text="位置:"},tip2={text="大小:"}})
       else
        table.insert(adapter_cascade.data,#adapter_cascade.data,{dir=p,pos={text="x=0:y=0",Visibility=View.INVISIBLE},size={Visibility=View.INVISIBLE,text="原大小"},tip={Visibility=View.INVISIBLE,text="位置:"},tip2={Visibility=View.INVISIBLE,text="大小:"}})
      end
      adapter_cascade.notifyDataSetChanged()
    end)
   else
    local items={}
    if unorder.checked
      items={"上移","下移","设置缩放大小","设置在底层视频上的位置","删除","预览"}
     else
      items={"上移","下移","删除","预览"}
    end
    AlertDialog.Builder(this).setTitle("请选择").setItems(items,{onClick=function(l,v)
        local s=items[v+1]
        if s=="上移"
          local data =adapter_cascade.data[d]
          if d-1>0
            table.remove(adapter_cascade.data,d)
            table.insert(adapter_cascade.data,d-1,data)
            adapter_cascade.notifyDataSetChanged()
           else
            print("最上边了")
          end
        end
        if s=="下移"
          local data =adapter_cascade.data[d]
          if d+2<=#adapter_cascade.data
            table.remove(adapter_cascade.data,d)
            table.insert(adapter_cascade.data,d+1,data)
            adapter_cascade.notifyDataSetChanged()
           else
            print("最下边了")
          end
        end
        if s=="删除"
          table.remove(adapter_cascade.data,d)
          adapter_cascade.notifyDataSetChanged()
        end
        if s=="预览"
          dialogPlayerVideo(activity,direction)
        end
        if s=="设置缩放大小"
          local lay={EditText,hint="例如:500:100，留空不变，支持+-*/%号和本视频宽高w，h等",id="et"}
          AlertDialog.Builder(this) .setTitle("请输入") .setView(loadlayout(lay)) .setPositiveButton("确定", {onClick=function()
              if d==1
                print("首个视频为底层，不允许更改")
                return
              end
              if et.text~=""
                adapter_cascade.data[d].size={text=et.text}
               else
                adapter_cascade.data[d].size={text="原大小"}
              end
              adapter_cascade.notifyDataSetChanged()
          end}) .setNegativeButton("取消", nil).show()
        end
        if s=="设置在底层视频上的位置"
          local lay={EditText,hint="例如:x=30:y=30，留空为x=0:y=0",id="et"}
          AlertDialog.Builder(this) .setTitle("请输入") .setView(loadlayout(lay)) .setPositiveButton("确定", {onClick=function()
              if d==1
                print("首个视频为底层，不允许更改")
                return
              end
              if et.text~=""
                adapter_cascade.data[d].pos={text=et.text}
               else
                adapter_cascade.data[d].pos={text="x=0:y=0"}
              end
              adapter_cascade.notifyDataSetChanged()
          end}) .setNegativeButton("取消", nil).show()
        end
    end}) .show()
  end
end
linear.checked=true
linear.text="线性拼接:横向"
linear.onClick=function()
  AlertDialog.Builder(this) .setTitle("请选择") .setMessage("消息") .setPositiveButton("横向",{onClick=function(v)
      unorder.checked=false
      linear.text="线性拼接:横向"
      for n=1,#adapter_cascade.data-1 do
        adapter_cascade.data[n].pos={Visibility=View.INVISIBLE}
        adapter_cascade.data[n].size={Visibility=View.INVISIBLE}
        adapter_cascade.data[n].tip={Visibility=View.INVISIBLE}
        adapter_cascade.data[n].tip2={Visibility=View.INVISIBLE}
      end
      adapter_cascade.notifyDataSetChanged()
      end}) .setNegativeButton("纵向",{onClick=function()
      unorder.checked=false
      linear.text="线性拼接:纵向"
      for n=1,#adapter_cascade.data-1 do
        adapter_cascade.data[n].pos={Visibility=View.INVISIBLE}
        adapter_cascade.data[n].size={Visibility=View.INVISIBLE}
        adapter_cascade.data[n].tip={Visibility=View.INVISIBLE}
        adapter_cascade.data[n].tip2={Visibility=View.INVISIBLE}
      end
      adapter_cascade.notifyDataSetChanged()
      end}) .setNeutralButton("取消",{onClick=function()
      unorder.checked=true
      linear.checked=false
      for n=1,#adapter_cascade.data-1 do
        adapter_cascade.data[n].pos={Visibility=View.VISIBLE}
        adapter_cascade.data[n].size={Visibility=View.VISIBLE}
        adapter_cascade.data[n].tip={Visibility=View.VISIBLE}
        adapter_cascade.data[n].tip2={Visibility=View.VISIBLE}
      end
      adapter_cascade.notifyDataSetChanged()
  end}) .show()
end
unorder.onClick=function()
  unorder.checked=true
  linear.checked=false
  for n=1,#adapter_cascade.data-1 do
    adapter_cascade.data[n].pos={Visibility=View.VISIBLE}
    adapter_cascade.data[n].size={Visibility=View.VISIBLE}
    adapter_cascade.data[n].tip={Visibility=View.VISIBLE}
    adapter_cascade.data[n].tip2={Visibility=View.VISIBLE}
  end
  adapter_cascade.notifyDataSetChanged()
end
start.background=btBackground
start. onClick=function()
  if filename.text~=""
    local i="-i "
    for n=1,#adapter_cascade.data-1 do
      if #adapter_cascade.data-1==n
        i=i..adapter_cascade.data[n].dir
       else
        i=i..adapter_cascade.data[n].dir.." -i "
      end
    end

    if unorder.checked
      local scale=""
      local overlay=""
      if #adapter_cascade.data==2
        print("只有一层视频")
        return
      end
      for n=2,#adapter_cascade.data-1 do
        local scale1=adapter_cascade.data[n].size.text
        local pos=adapter_cascade.data[n].pos.text
        if scale1=="原大小"
          scale=scale.. "[".. tostring(n-1).."]scale=-1:-1[t".. tostring(n-1).."];"
         else
          scale=scale.. "[".. tostring(n-1).."]scale=".. tostring(scale1).."[t".. tostring(n-1).."];"
        end
        if #adapter_cascade.data-1==2
          overlay="[0][t1]overlay=".. tostring(pos)
         else
          if n==#adapter_cascade.data-1
            overlay=overlay.. "[T".. tostring(n-2).."][t"..tostring(n-1).."]overlay=".. tostring(pos)
           else
            if n-2==0
              overlay=overlay.. "[0][t"..tostring(n-1).."]overlay=".. tostring(pos).."[T"..tostring(n-1).."];"
             else
              overlay=overlay.. "[T".. tostring(n-2).."][t"..tostring(n-1).."]overlay=".. tostring(pos).."[T"..tostring(n-1).."];"
            end
          end
        end
      end
      local cmd1= i.." -lavfi "..scale..overlay.." -an -y "..LUA_SD.."/avHandle/tmp/mix_v.ts"
      local cmd2= i.." -lavfi amix=inputs="..tostring(#adapter_cascade.data-1).." -y -vn "..LUA_SD.."/avHandle/tmp/mix_a.ts"
      local cmd3= "-i "..LUA_SD.."/avHandle/tmp/mix_v.ts -i "..LUA_SD.."/avHandle/tmp/mix_a.ts -y " ..oDir.text..filename.text.."."..sp.getSelectedItem()
      activity.newActivity("handle",{cmd1,cmd2,cmd3})
     else
      local cmd=""
      local cmd1= i.." -lavfi amix=inputs="..tostring(#adapter_cascade.data-1).." -y -vn "..LUA_SD.."/avHandle/tmp/mix_a.ts"
      if linear.text=="线性拼接:横向"
        cmd=i.." -lavfi hstack=inputs="..tostring(#adapter_cascade.data-1).." -y -an "..LUA_SD.."/avHandle/tmp/mix_v.ts"
       else
        cmd=i.." -lavfi vstack=inputs="..tostring(#adapter_cascade.data-1).." -y -an "..LUA_SD.."/avHandle/tmp/mix_v.ts"..sp.getSelectedItem()
      end
      local cmd2="-i "..LUA_SD.."/avHandle/tmp/mix_a.ts -i "..LUA_SD.."/avHandle/tmp/mix_v.ts -y "..oDir.text..filename.text.."."..sp.getSelectedItem()
      activity.newActivity("handle",{cmd,cmd1,cmd2})
    end
   else
    print("请输入文件名")
  end
end